package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.Certificate;
import com.blb.service.ICertificateService;
import com.blb.service.IUserService;
import com.blb.util.RandomUtils;
import com.blb.util.ResponseHelp;
import net.sf.json.JSONObject;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZhengSanNan
 * @since 2019-12-24
 */
@RestController
@RequestMapping("/certificate")
public class CertificateController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /*@Resource
    private RestTemplate restTemplate1;*/

    @Autowired
    private ICertificateService certificateService;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @Resource
    private IUserService userService;


    @PostMapping("updatePwd")
    public ResponseHelp updatePwd(String id,String password,String password1,String password2){
        Certificate certificate = certificateService.getById(id);
        if (!certificate.getToken().equals(password)) {
            return ResponseHelp.error("原密码错误，请重新输入");
        }
        if (!password1.equals(password2)) {
            return ResponseHelp.error("两次输入新密码不一致，请重新输入");
        }
        certificate.setToken(password1);
        certificateService.updateById(certificate);
        return ResponseHelp.success("修改成功");
    }

    @PostMapping("login")
    public ResponseHelp login(String certificate, String token) {
        Certificate certificate1 = certificateService.findUserByCertificate(certificate);
        System.out.println(certificate1);
        if (certificate1 == null) {
            return ResponseHelp.error("用户名不存在");
        }
        if (!certificate1.getToken().equals(token)) {
            return ResponseHelp.error("密码错误");
        }
        System.out.println(certificate1.getId());
        redisTemplate.opsForValue().set("accessToken", certificate1.getId());
        return ResponseHelp.success(certificate1.getId());
    }

    @PostMapping("mp_login")
    public ResponseHelp mpLogin(@RequestBody Certificate certificate) {
        Certificate certificate1 = certificateService.findUserByCertificate(certificate.getCertificate());
        System.out.println(certificate1);
        if (certificate1 == null) {
            return ResponseHelp.error("用户名不存在");
        }
        if (!certificate1.getToken().equals(certificate.getToken())) {
            return ResponseHelp.error("密码错误");
        }
        System.out.println(certificate1.getId());
        redisTemplate.opsForValue().set("accessToken", certificate1.getId());
        return ResponseHelp.success(certificate1.getId());
    }


/*
    @GetMapping("getCode")
    public ResponseHelp getCode(String certificate) {
        Certificate email1 = certificateService.getOne(new QueryWrapper<Certificate>().eq("certificate", certificate));

        if (null == email1) {
            String random = RandomUtils.random();
            rabbitTemplate.convertAndSend("BaseQueue", random + "," + certificate);
            redisTemplate.opsForValue().set("register"+certificate, random);
            return ResponseHelp.success("验证码已发送至邮箱");
        }
        return ResponseHelp.error("该邮箱已被注册");
    }

    @PostMapping("register")
    public ResponseHelp register(@RequestBody Certificate certificate) {
        String id = UUIDUtils.getId();
        certificate.setId(id);
        */
/**
         * 通过上面的 redisTemplate.opsForValue().set(certificate, random); key-value
         * 通过邮箱找到验证码   这样防止了恶意注册  获取验证码后换个邮箱  或恶意（操作邮箱验证码）
         * 防止注册 登录操作有误  不让它覆盖彼此的验证码
         *//*

        Object o = redisTemplate.opsForValue().get("register"+certificate.getCertificate());

        if (o.toString().equals(certificate.getCode())) {

            boolean save = certificateService.save(certificate);

            if(save){
                User user = new User();
                user.setId(id);
                user.setEmail(certificate.getCertificate());
                user.setSex("保密");
                user.setNickname(UUIDUtils.getId());
                user.setImg("img/timg.jpg");
              */
/*  User user1 = restTemplate1.postForObject("http://note-user/user/saveUser",
                        user, User.class);
*//*

                userService.save(user);
                return ResponseHelp.success("注册成功");
            }
            return ResponseHelp.error("注册失败");

        }

        return ResponseHelp.error("邮箱注册失败");
    }
*/



    @PostMapping("update")
    public ResponseHelp update(@RequestBody Certificate certificate){
        Certificate certificate1 = certificateService.findUserByCertificate(certificate.getCertificate());
        System.out.println(certificate1);
        if(certificate1==null){
            return ResponseHelp.error("此邮箱没有注册");
        }
        certificateService.updateToken(certificate.getCertificate(),certificate.getToken());
        return ResponseHelp.success("重置成功,请返回首页登录");
    }


    @GetMapping("getCode1")
    public ResponseHelp getCode1(String certificate) {
        System.out.println("qwe");
        Certificate email1 = certificateService.getOne(new QueryWrapper<Certificate>().eq("certificate", certificate));

        if (email1!= null) {
            String random = RandomUtils.random();
            rabbitTemplate.convertAndSend("BaseQueue", random + "," + certificate);
            redisTemplate.opsForValue().set("login"+certificate, random);
            return ResponseHelp.success("验证码已发送至邮箱");
        }
        return ResponseHelp.error("该邮箱没注册，请前往注册页面注册");

    }


    @PostMapping("login1")
    public ResponseHelp login1(@RequestBody Certificate certificate) {
        Object o = redisTemplate.opsForValue().get("login"+certificate.getCertificate());

        if (o.toString().equals(certificate.getToken())){
            Certificate certificate1 = certificateService.findUserByCertificate(certificate.getCertificate());
                redisTemplate.opsForValue().set("accessToken", certificate1.getId());
                return ResponseHelp.success(certificate1.getId());
        }

        return ResponseHelp.error("邮箱注册失败");
    }
/*

    */
/* 码云登录 *//*

    @GetMapping("mayuntest")
    public ResponseHelp maYun(String code){
        String accessToken = MaYunUtil.getAccessToken(code);
        JSONObject object = MaYunUtil.getObject(accessToken);
        String id = object.getString("id");
        System.out.println(object);
        return ResponseHelp.success("aaa");

    }
*/

}

